package org.apache.dolphinscheduler.api.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.UserBalanceDetailService;
import org.apache.dolphinscheduler.api.service.UsersService;
import org.apache.dolphinscheduler.api.utils.DateUtil;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.api.vo.UserBalanceVo;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.entity.UserBalanceDetail;
import org.apache.dolphinscheduler.dao.mapper.UserBalanceDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class UserBalanceDetailServiceImpl extends ServiceImpl<UserBalanceDetailMapper, UserBalanceDetail> implements UserBalanceDetailService {

	@Autowired
	private UsersService usersService;
	
	public Result<Object> queryBalanceDetail(User loginUser, UserBalanceVo vo) {
		Result<Object> result = new Result<>();
		
		QueryWrapper<UserBalanceDetail> qw = new QueryWrapper<UserBalanceDetail>();
        if(StringUtils.isNotBlank(vo.getTypes())) {
        	qw.eq("types", vo.getTypes());
        }
        if(StringUtils.isNotBlank(vo.getUserId())) {
        	qw.eq("user_id", vo.getUserId());
        }
        if(StringUtils.isNotBlank(vo.getTransType())) {
        	qw.eq("trans_type", vo.getTransType());
        }
        if(StringUtils.isNotBlank(vo.getBeginTime())) {
			Date beginDate = DateUtil.formatDate(vo.getBeginTime() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
			qw.ge("apply_time", beginDate);
		}
        if(StringUtils.isNotBlank(vo.getEndTime())) {
			Date endDate = DateUtil.formatDate(vo.getEndTime() + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
			qw.le("apply_time", endDate);
		}
		if(loginUser.getRoleId() != null && loginUser.getRoleId() != 1) {
			qw.eq("user_id", loginUser.getId());
		}
        
        qw.orderByDesc("trans_time");
        Page<UserBalanceDetail> page = new Page<>(vo.getPageNo(), vo.getPageSize());
		page = page(page, qw);
		if(page.getRecords() != null && page.getRecords().size() > 0) {
			for(UserBalanceDetail up : page.getRecords()) {
				User user = usersService.queryUser(up.getUserId());
				up.setUserName(user != null ? user.getUserName() : "");
			}
		}
		PageInfo<UserBalanceDetail> pageInfo = new PageInfo<>(vo.getPageNo(), vo.getPageSize());
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setTotalList(page.getRecords());
        result.setData(pageInfo);
        
        result.setCode(Status.SUCCESS.getCode());
    	result.setMsg(Status.SUCCESS.getMsg());
        return result;
	}
}
